package servlet;

import entity.Student;
import entity.User;
import service.StudentService;
import service.UserService;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "LoginServlet",urlPatterns = "/login")
public class LoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        System.out.println("username:"+username+" password:"+password);
        UserService userService = new UserService();
        User user1= userService.loginService(new User(username,password));
        if (null != user1){
            System.out.println("登陆成功！！！");
            request.getSession().setAttribute("user",user1);
            /*StudentService studentService = new StudentService();
            List<Student> students = studentService.getStudentListService(0, 2);
            request.getSession().setAttribute("students",students);
            response.sendRedirect("student.jsp");*/
            response.sendRedirect("students");
        }else {
            System.out.println("登录失败！！！");
            request.setAttribute("error","用户名或者密码错误！！！");
            RequestDispatcher requestDispatcher = request.getRequestDispatcher("login.jsp");
            requestDispatcher.forward(request,response);
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
}
